﻿//Copyright (c) 2010 William Liu, Enoch Ye
//William Liu <william.lyz@gmail.com>
//Enoch Ye <moonlight.enoch@gmail.com>

//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at

//       http://www.apache.org/licenses/LICENSE-2.0

//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GBG_DBM.Support.DAL.Entities;
using GBG_DBM.Support.DAL;
using GBG_DBM.Support.DAL.DB;

namespace GBG_DBM.UI
{
    public partial class Supplier_RM_UI : Form
    {
        private int CurrentRowIndex;
        private DataTable _CurrentIngredients;

        public DataTable CurrentIngredients
        {
            get
            {
                if (_CurrentIngredients == null)
                {
                    SetCurrentIngredients();
                }
                return _CurrentIngredients;
            }
            set { _CurrentIngredients = value; }
        }
        private Ingredient _CurrentIngredient;

        public Ingredient CurrentIngredient
        {
            get { return _CurrentIngredient; }
            set { _CurrentIngredient = value; }
        }
        private RawMaterialSupplier _RawMaterialSupplier = new RawMaterialSupplier();
        private DBAccessory _DBAccessory = new DBAccessory();
        public Supplier_RM_UI()
        {
            InitializeComponent();
            //this.Show();
            txtSupplierAgency.Enabled = false;
        }
		public Supplier_RM_UI(Supplier_RM CurrentSupplierRM)
		{
			InitializeComponent();
			cbbSupplierCode.Text = CurrentSupplierRM.SupplierCode;
			txtSupplierAgency.Text = CurrentSupplierRM.SupplierName;
            _CurrentIngredients = null;
			CurrentIngredients.Rows.Add(CurrentSupplierRM.Code, CurrentSupplierRM.SupplierName, CurrentSupplierRM.SupplierCode,
				CurrentSupplierRM.INCIName, CurrentSupplierRM.TradeName, CurrentSupplierRM.KeyProperty, CurrentSupplierRM.LocalPrice,
				CurrentSupplierRM.Category);
			cbbCurrency.Text = CurrentSupplierRM.CurrencyName;
			//lblCurrency.Text = Convert.ToString(CurrentSupplierRM.EstimatePrice);
			cbbFreightType.Text = CurrentSupplierRM.FreightType;
			cbbEstimateLoss.Text = Convert.ToString(CurrentSupplierRM.EstimatedLoss);
			txtPackSize.Text = Convert.ToString(CurrentSupplierRM.Packsize);
			dtpFrom.Text = CurrentSupplierRM.EstimateDateFrom;
			dtpTo.Text = CurrentSupplierRM.EstimateDateTo;

		}
        private void AddRowToIngredientGrid(DataTable Ingredients, DataRowView drv) 
        {
            if (Ingredients != null) 
            {
                Ingredients.Rows.Add(drv["Code"], drv["Supplier Code"], drv["Supplier_Name"], drv["INCINAME"], drv["TradeName"], drv["KeyProperty"],
                    drv["LocalPrice"], drv["Category"]);
            }
        }
        private void SetFromControlAndLoadData() 
        {
            this.dgvIngredients.DataSource = getIngredientsBySupplier();
            this.dgvIngredients.Visible = true;
            this.dgvIngredients.ReadOnly = true;
        }
        private DataTable getIngredientsBySupplier() 
        {
            IngredientDB _Ingredient = new IngredientDB();
            DataTable dt = _Ingredient.getIngredientsBySupplier(cbbSupplierCode.Text);
            CurrentIngredients = null;
            foreach (DataRowView drv in dt.DefaultView) 
            {
                AddRowToIngredientGrid(CurrentIngredients, drv);
            }
            return _CurrentIngredients;
        }
        private void SetDataGridFormat() 
        {
            //dgvIngredient
            this.dgvIngredients.Columns["Code"].HeaderText = "Ingredient Code";
            this.dgvIngredients.Columns["Supplier_Name"].HeaderText = "Supplier Name";
            this.dgvIngredients.Columns["Supplier_Code"].HeaderText = "Supplier Code";
            this.dgvIngredients.Columns["INCIName"].HeaderText = "INCI Name";
            this.dgvIngredients.Columns["TradeName"].HeaderText = "Trade Name";
            this.dgvIngredients.Columns["KeyProperty"].HeaderText = "Key Property";
            this.dgvIngredients.Columns["LocalPrice"].HeaderText = "Local Price";
            this.dgvIngredients.Columns["Category"].HeaderText = "Category";
        }
        public Supplier_RM_UI(DataTable raw, DataTable cur, DataTable freight, DataTable ingredient) 
        {
            InitializeComponent();
            this.RetrieveData(raw, cur, freight, ingredient);
            txtSupplierAgency.Enabled = false;
        }
        private void SetCurrentIngredients() 
        {
            _CurrentIngredients = new DataTable("Ingredient");
            _CurrentIngredients.Columns.Add("Code");
            _CurrentIngredients.Columns.Add("Supplier_Code");
            _CurrentIngredients.Columns.Add("Supplier Name");
            _CurrentIngredients.Columns.Add("INCINAME");
            _CurrentIngredients.Columns.Add("TradeName");
            _CurrentIngredients.Columns.Add("KeyProperty");
            _CurrentIngredients.Columns.Add("LocalPrice");
            _CurrentIngredients.Columns.Add("Category");
        }
        private void RetrieveData(DataTable raw, DataTable cur, DataTable freight, DataTable ingredient) 
        {
            int rawcount = raw.Rows.Count;
            int curcount = cur.Rows.Count;
            int freightcount = freight.Rows.Count;
            int ingredientcount = ingredient.Rows.Count;
            string[] Loss=new string[]{"0.1","0.2","0.3","0.4","0.5"};
            for(int t=0;t<rawcount;t++){
                cbbSupplierCode.Items.Add(Convert.ToString(raw.Rows[t].ItemArray[0]));
            }
            for (int a = 0; a < curcount; a++) 
            {
                cbbCurrency.Items.Add(Convert.ToString(cur.Rows[a].ItemArray[0]));
            }
            for (int b = 0; b < freightcount; b++) 
            {
                cbbFreightType.Items.Add(Convert.ToString(freight.Rows[b].ItemArray[0]));
            }
            cbbEstimateLoss.Items.AddRange(Loss);
            cbbEstimateLoss.Text = Convert.ToString(cbbEstimateLoss.Items[0]);
            cbbSupplierCode.Text = Convert.ToString(cbbSupplierCode.Items[0]);
            txtSupplierAgency.Text = _DBAccessory.getRawMaterialSupplierByCode(cbbSupplierCode.Text).Name;
            cbbCurrency.Text = Convert.ToString(cbbCurrency.Items[0]);
            //lblCurrency.Text = Convert.ToString(_DBAccessory.getCURbyCurrency(cbbCurrency.Text).NewZealandDollar);
            cbbFreightType.Text = Convert.ToString(cbbFreightType.Items[0]);

        }
        private void Cancel_Supplier_UI_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Supplier_RM_UI_Load(object sender, EventArgs e)
        {
            this.SetFromControlAndLoadData();
        }

        private void cbbSupplierCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selected;
            selected = cbbSupplierCode.SelectedItem.ToString();
            txtSupplierAgency.Text = _DBAccessory.getRawMaterialSupplierByCode(selected).Name;
            SetFromControlAndLoadData();

        }
        private void dgvIngredients_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            string CurrentIngredient;
            _CurrentIngredient = new Ingredient();
            if (e.RowIndex > -1) 
            {
                CurrentRowIndex = e.RowIndex;
                this.dgvIngredients.Rows[CurrentRowIndex].Selected = true;
                CurrentIngredient = (string)this.dgvIngredients[0, CurrentRowIndex].Value;
                _DBAccessory = new DBAccessory();
                _CurrentIngredient = _DBAccessory.getIngredientByCode(CurrentIngredient);
            }
        }
        private void OK_Supplier_RM_UI_Click(object sender, EventArgs e)
        {
            if (_CurrentIngredient != null)
            {
                string Dateform=dtpFrom.Value.ToString();
                string DataTo=dtpTo.Value.ToString();
                string Pack=txtPackSize.Text;
                string EstimateLoss=cbbEstimateLoss.Text;
                string Fre=cbbFreightType.Text;
                string Cur=cbbCurrency.Text;
                string EstimatedPrice = lblCurrency.Text;
                Supplier_RM_View_UI SupplierView = new Supplier_RM_View_UI(_CurrentIngredient, Cur, EstimatedPrice, Fre, EstimateLoss, Pack, DataTo, Dateform);
                SupplierView.Show();
            }    
        }
        private void cbbCurrency_SelectedIndexChanged(object sender, EventArgs e)
        {
                string selectd;
                int price = Convert.ToInt32(_CurrentIngredients.Rows[0].ItemArray[6]);
                selectd = cbbCurrency.Text;
                float currency=_DBAccessory.getCURbyCurrency(selectd).NewZealandDollar;
                lblCurrency.Text = Convert.ToString(currency * price);
            
        }
    }
}
